﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcSiteMapProvider;
using HotDealD2.Helper;
using System.Web.Security;
using HotDealD2.Models.ModelView;
using HotDealD2.Models.ModelManager;
using HotDealD2.Models;
namespace HotDealD2.Controllers
{
    
    public class HomeController : Controller
    {
        public ActionResult Index()
        {

            //var list = new List<String>();
            //var giaTien = new List<String>();
            //var giaGoc = new List<String>();
            //var tietkiem = new List<int>();
            //DealModel dealmodel = new DealModel();
            //IList<Deal> listdeal = dealmodel.GetAllDeal();
            //for (int i = 0; i < listdeal.Count; i++)
            //{
            //    list.Add(String.Format("{0:0,0}",(listdeal[i].GiaGoc - listdeal[i].GiaTien)));
            //    giaTien.Add(String.Format("{0:0,0}", listdeal[i].GiaTien));
            //    giaGoc.Add(String.Format("{0:0,0}", listdeal[i].GiaGoc));
            //    tietkiem.Add(Decimal.ToInt32((listdeal[i].GiaGoc - listdeal[i].GiaTien)) * 100 / Decimal.ToInt32(listdeal[i].GiaGoc) );
            //}
            //ViewBag.GiaGiam = list;
            //ViewBag.GiaTien = giaTien;
            //ViewBag.GiaGoc = giaGoc;
            //ViewBag.TietKiem = tietkiem;
            //return View(listdeal);
            DealModel dealmodel = new DealModel();
            IList<ALLDealView> listdeal = dealmodel.GetAllDeal(0);
            return View(listdeal);

        }

        
        public ActionResult IndexType(int id)
        {
            DealModel dealmodel = new DealModel();
            IList<ALLDealView> listdeal = dealmodel.GetAllDeal(id);
            return View(listdeal);
        }
       
        
        public ActionResult About()
        {
            return View();
        }

        public ActionResult DealDetail(int id)
        {
            DealCommentView dealcommentview = new DealCommentView();
            DealModel dealmodel = new DealModel();
            CommentModel commentmodel = new CommentModel();
            Deal deal = dealmodel.GetDeal(id);
            String giaTien =String.Format("{0:0,0}", deal.GiaTien);
            String giaGoc =String.Format("{0:0,0}",deal.GiaGoc);
            String tietKiem =String.Format("{0:0,0}",(double)(100 - (deal.GiaTien / deal.GiaGoc)*100));
            ViewBag.TietKiem =tietKiem;
            ViewBag.GiaTien = giaTien;
            ViewBag.GiaGoc = giaGoc;

            dealcommentview.Comment = commentmodel.GetCommentByDeal(deal.MaDeal); 
            dealcommentview.Deal = deal;

            ///// code get url google
            String imageUrl = "";
            imageUrl = "http://maps.googleapis.com/maps/api/staticmap?center=";
            String soNha = deal.ThanhVien.NguoiDung.SoNha;
            imageUrl += soNha;
            imageUrl += ",";
            String duong = deal.ThanhVien.NguoiDung.Duong;
            imageUrl += duong;
            imageUrl += ",";
            imageUrl += "ho chi minh, viet nam";
            imageUrl += "&zoom=16";
            imageUrl += "&size=600x300";
            imageUrl += "&maptype=roadmap";
            imageUrl += "&markers=color:red%7Clabel:A%7C";
            imageUrl += soNha;
            imageUrl += ",";
            imageUrl += duong;
            imageUrl += ",";
            imageUrl += "ho chi minh, viet nam";
            imageUrl += "&sensor=false";
            imageUrl = imageUrl.Replace(' ','%');
            
            dealcommentview.ImageURLGoogle = imageUrl;


            /////
            return View(dealcommentview);

        }

        [HttpPost]
        public ActionResult RegisterEmail(String email)
        {
            AccountModel account = new AccountModel();
           // string email1 = Session["email"].ToString();
            if (email.Contains("@"))
            {
                EmailNhanDeal newEmail = new EmailNhanDeal();
                newEmail.Email = email;

                QLDataBaseDataContext datacontext = new QLDataBaseDataContext();
                bool result = account.IsEmailAddressGetDealExist(email);
                if (result)
                {
                    ModelState.AddModelError("", "Địa chỉ E-mail đã đăng ký");
                
                }
                else
                {
                    datacontext.EmailNhanDeals.InsertOnSubmit(newEmail);
                    datacontext.SubmitChanges();
                    ModelState.AddModelError("", "Đăng ký thành công");
                }




            }
            else
            {
                ModelState.AddModelError("", "Địa chỉ E-mail không đúng");
            }
            return RedirectToAction("Index");
        }

        [MvcSiteMapNode(Title = "Ăn uống", ParentKey = "DealMoi")]
        public ActionResult AnUong()
        {
            return View();
        }
        [MvcSiteMapNode(Title = "Làm đẹp", ParentKey = "DealMoi")]
        public ActionResult LamDep()
        {
            return View();
        }

        [MvcSiteMapNode(Title = "Giải trí", ParentKey = "DealMoi")]
        public ActionResult GiaiTri()
        {
            return View();
        }

        [MvcSiteMapNode(Title = "Đào tạo", ParentKey = "DealMoi")]
        public ActionResult DaoTao()
        {
            return View();
        }
        [MvcSiteMapNode(Title = "Sản phẩm", ParentKey = "DealMoi")]
        public ActionResult SanPham()
        {
            return View();
        }

        [MvcSiteMapNode(Title = "Du lịch", ParentKey = "DealMoi")]
        public ActionResult DuLich()
        {
            return View();
        }

        [MvcSiteMapNode(Title = "Deal du lịch", ParentKey = "Home")]
        public ActionResult DealDuLich()
        {
            return View();
        }
        [MvcSiteMapNode(Title = "Deal gần đây", ParentKey = "Home")]
        public ActionResult DealGanDay()
        {
            return View();
        }

        public ActionResult ThaoLuan()
        {
            return View();
        }

        public ActionResult Enquiry()
        {
            return View();
        }

        public ActionResult SiteMap()
        {
            return View();
        }
        public RedirectToRouteResult AddToCart(int maDeal)
        {
            if (Session["LogOn"] == null)
            {
                return RedirectToAction("Logon","Account");
            }
            DealModel dealmodel = new DealModel();
            Deal deal = dealmodel.GetDeal(maDeal);
            if (deal != null)
                GetCart().AddItem(deal, 1);
            return RedirectToAction("ShoppingCart","Member");

        }

        public RedirectToRouteResult Discuss(int maDeal,FormCollection collection)
        {
           // int maDeal = int.Parse(collection.Get("id"));
            if (Session["LogOn"] == null)
            {
                return RedirectToAction("Logon", "Account");
            }
            DealModel dealmodel = new DealModel();
            CommentModel model = new CommentModel();
            Deal deal = dealmodel.GetDeal(maDeal);
            CommentView temp = new CommentView();
            temp.MaDeal =deal.MaDeal;
            temp.Username=(String)Session["Logon"];
            temp.NoiDung = collection.Get("noidung");
            bool result = model.InsertComment(temp);
            if (result)
            {
                return RedirectToAction("DealDetail", "Home", new { id = deal.MaDeal });
            }
            return RedirectToAction("Index");

        }

        public CartModel GetCart()
        {
            CartModel cart = (CartModel)Session["Cart" +Session["Logon"]];
            if (cart == null)
            {
                cart = new CartModel();
                Session["Cart" + Session["Logon"]] = cart;
            }
            return cart;
            
        }

        public ActionResult ShoppingCart()
        {
            CartModel Cart =GetCart();
            return View(Cart);
        }
    }
}
